package com.woniu.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.Bank;
import com.woniu.entity.ProSpence;
import com.woniu.entity.wrapperinterface.AllProSpence;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author hyl,fzn
 * @since 2022-08-28
 * 这里暂时不使用mybatis-plus的方法
 */
public interface ProSpenceMapper extends BaseMapper<ProSpence> {

    /**
     * 增加项目流水
     * @param proSpence
     * spenPrice大于等于0，项目id以及花销种类id应为有效值
     */
    @Insert("insert into " +
            "pro_spence(pro_id, cate_id, spen_date, spen_price) " +
            "values(#{proId},#{cateId},#{spenDate},#{spenPrice})")
    void addProSpence(ProSpence proSpence);


    /**
     * 可以根据流水id修改
     * 也可以根据流水id和项目id修改
     * @param proSpence
     */
    void updateProSpence(ProSpence proSpence);


    /**
     * 根据流水id查流水，此方法也可以根据流水id以及项目id查流水
     *
     * @param proSpence
     */
    ProSpence selectOneSpence(ProSpence proSpence);

    @Select("select * from pro_spence where spen_id=#{spenId}")
    ProSpence selectById(Integer id);
    /**
     * 根据项目id查询一个或者多个流水
     * @param proSpence
     * @return 返回一个列表
     * todo 后面会有项目具体信息的懒加载
     * todo 等待Project增加的方法的完成
     */
    List<ProSpence> selectProSpences(ProSpence proSpence);

    /**
     * 根据流水id删除流水数据
     * @param proSpence
     */
    @Delete("delete from pro_spence where spen_id=#{spenId}")
    void deleteProSpence(ProSpence proSpence);


    /**
     * 用于项目流水的分页查询，连表查询，可以带出项目的详细信息以及分类信息
     * @param page 分页的详细信息
     * @param wrapper sql条件语句的包装对象
     * @return 返回一个带有数据的Page(实现了IPage接口)
     */
    @Select("select ps.pro_id pro_id2,ps.spen_id,ps.cate_id,ps.spen_date,ps.spen_price,sc.cate_name,p.* " +
            "from pro_spence ps,project p,spen_category sc ${ew.customSqlSegment} " +
            "and ps.pro_id=p.pro_id and ps.cate_id=sc.cate_id")
    @Results(id="proSpenceWithProjectMap",value={
            @Result(column = "pro_id2",property = "proId"),
            @Result(column = "spen_id",property = "spenId"),
            @Result(column = "cate_id",property = "cateId"),
            @Result(column = "spen_date",property = "spenDate"),
            @Result(column = "spen_price",property = "spenPrice"),
            @Result(column = "pro_id",property = "targetProject.proId"),
            @Result(column = "bank_id",property = "targetProject.bank",
                    one = @One(select = "com.woniu.mapper.BankMapper.selectBankById")),
            @Result(column = "user_id",property = "targetProject.user",
                    one = @One(select="com.woniu.mapper.UserMapper.getByUserId")),
            @Result(column = "pro_name",property = "targetProject.proName"),
            @Result(column = "pro_id",property = "targetProject.proId"),
            @Result(column = "contract",property = "targetProject.contract"),
            @Result(column = "pro_start_time",property = "targetProject.proStartTime"),
            @Result(column = "pro_end_time",property = "targetProject.proEndTime"),
            @Result(column = "pre_price",property = "targetProject.prePrice"),
            @Result(column = "leftPrice",property = "targetProject.leftPrice"),
            @Result(column = "pro_status",property = "targetProject.proStatus"),
            @Result(column = "cate_id",property = "targetCategory",
            one = @One(select="com.woniu.mapper.SpenCategoryMapper.selectOneByCateId")),
    })
    Page<ProSpence> getAnyProSpenceWithPage(Page<ProSpence> page, @Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select ps.pro_id pro_id2,ps.spen_id,ps.cate_id,ps.spen_date,ps.spen_price,sc.cate_name,p.* " +
            "from pro_spence ps,project p,spen_category sc ${ew.customSqlSegment} " +
            "and ps.pro_id=p.pro_id and ps.cate_id=sc.cate_id")
    @ResultMap("proSpenceWithProjectMap")
    List<ProSpence> getAnyProSpence(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select ps.pro_id pro_id2,ps.spen_id,ps.cate_id,ps.spen_date,ps.spen_price,sc.cate_name,p.* " +
            "from pro_spence ps,project p,spen_category sc where " +
            " ps.pro_id=p.pro_id and ps.cate_id=sc.cate_id")
    @ResultMap("proSpenceWithProjectMap")
    Page<ProSpence> getAllProSpenceWithPage(Page<ProSpence> page);
}


